
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Guardian(基础) · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="poolboy.html" />
    
    
    <link rel="prev" href="../specifics/debugging.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../basics/basics.html">
            
                <a href="../basics/basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../basics/collections.html">
            
                <a href="../basics/collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../basics/enum.html">
            
                <a href="../basics/enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../basics/pattern-matching.html">
            
                <a href="../basics/pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../basics/control-structures.html">
            
                <a href="../basics/control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../basics/functions.html">
            
                <a href="../basics/functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../basics/pipe-operator.html">
            
                <a href="../basics/pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../basics/modules.html">
            
                <a href="../basics/modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../basics/mix.html">
            
                <a href="../basics/mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../basics/sigils.html">
            
                <a href="../basics/sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../basics/documentation.html">
            
                <a href="../basics/documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../basics/testing.html">
            
                <a href="../basics/testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../basics/comprehensions.html">
            
                <a href="../basics/comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../basics/strings.html">
            
                <a href="../basics/strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../basics/date-time.html">
            
                <a href="../basics/date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../basics/mix-tasks.html">
            
                <a href="../basics/mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../basics/iex-helpers.html">
            
                <a href="../basics/iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../advanced/erlang.html">
            
                <a href="../advanced/erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../advanced/error-handling.html">
            
                <a href="../advanced/error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../advanced/escripts.html">
            
                <a href="../advanced/escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../advanced/concurrency.html">
            
                <a href="../advanced/concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../advanced/otp-concurrency.html">
            
                <a href="../advanced/otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../advanced/otp-supervisors.html">
            
                <a href="../advanced/otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../advanced/otp-distribution.html">
            
                <a href="../advanced/otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../advanced/metaprogramming.html">
            
                <a href="../advanced/metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../advanced/umbrella-projects.html">
            
                <a href="../advanced/umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../advanced/typespec.html">
            
                <a href="../advanced/typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="../advanced/behaviours.html">
            
                <a href="../advanced/behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="../advanced/gen-stage.html">
            
                <a href="../advanced/gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="../advanced/protocols.html">
            
                <a href="../advanced/protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="../advanced/nerves.html">
            
                <a href="../advanced/nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter active" data-level="5.1" data-path="guardian.html">
            
                <a href="guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="poolboy.html">
            
                <a href="poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="benchee.html">
            
                <a href="benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="bypass.html">
            
                <a href="bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="distillery.html">
            
                <a href="distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="stream-data.html">
            
                <a href="stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Guardian(基础)</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p><a href="https://github.com/ueberauth/guardian" target="_blank">Guardian</a> &#x662F;&#x4E00;&#x4E2A;&#x5E7F;&#x6CDB;&#x4F7F;&#x7528;&#x7684;&#xFF0C;&#x57FA;&#x4E8E; <a href="https://jwt.io/" target="_blank">JWT</a> (JSON Web Tokens) &#x7684;&#x9A8C;&#x8BC1;&#x7A0B;&#x5E8F;&#x5E93;&#x3002;  </p>
<h2 id="jwts">JWTs</h2>
<p>&#x4E00;&#x4E2A; JWT &#x80FD;&#x4E3A;&#x9A8C;&#x8BC1;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x4E86;&#x4E30;&#x5BCC;&#x7684;&#x4FE1;&#x606F;&#x7684;&#x4EE4;&#x724C;&#x3002;&#x4E0D;&#x50CF;&#x5176;&#x5B83;&#x9A8C;&#x8BC1;&#x7CFB;&#x7EDF;&#xFF0C;&#x53EF;&#x80FD;&#x53EA;&#x63D0;&#x4F9B;&#x4E86;&#x8D44;&#x6E90;&#x4E3B;&#x4F53;&#x7684;&#x6807;&#x8BC6;&#x7B26;&#xFF0C;JWTs &#x9664;&#x6B64;&#x4E4B;&#x5916;&#x8FD8;&#x80FD;&#x63D0;&#x4F9B;&#x5982;&#x4E0B;&#x4FE1;&#x606F;&#xFF1A;  </p>
<ul>
<li>&#x8C01;&#x7B7E;&#x53D1;&#x7684;&#x4EE4;&#x724C;  </li>
<li>&#x4EE4;&#x724C;&#x7684;&#x62E5;&#x6709;&#x8005;&#x662F;&#x8C01;  </li>
<li>&#x54EA;&#x4E2A;&#x7CFB;&#x7EDF;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x4EE4;&#x724C;  </li>
<li>&#x4EE4;&#x724C;&#x7B7E;&#x53D1;&#x7684;&#x65F6;&#x95F4;  </li>
<li>&#x4EE4;&#x724C;&#x8FC7;&#x671F;&#x7684;&#x65F6;&#x95F4;  </li>
</ul>
<p>&#x9664;&#x4E86;&#x8FD9;&#x4E9B;&#x5B57;&#x6BB5;&#x4EE5;&#x5916;&#xFF0C;Guardian &#x8FD8;&#x63D0;&#x4F9B;&#x4E86;&#x53E6;&#x4E00;&#x4E9B;&#x5B57;&#x6BB5;&#x6765;&#x8F85;&#x52A9;&#x5176;&#x5B83;&#x4E00;&#x4E9B;&#x529F;&#x80FD;&#x7684;&#x4F7F;&#x7528;&#xFF1A;  </p>
<ul>
<li>&#x4EE4;&#x724C;&#x7684;&#x7C7B;&#x578B;&#x662F;&#x4EC0;&#x4E48;  </li>
<li>&#x4EE4;&#x724C;&#x7684;&#x62E5;&#x6709;&#x8005;&#x5177;&#x6709;&#x54EA;&#x4E9B;&#x6743;&#x9650;  </li>
</ul>
<p>&#x8FD9;&#x4E9B;&#x53EA;&#x662F; JWT &#x91CC;&#x9762;&#x7684;&#x57FA;&#x7840;&#x5B57;&#x6BB5;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x4EFB;&#x610F;&#x6DFB;&#x52A0;&#x4F60;&#x7684;&#x5E94;&#x7528;&#x9700;&#x8981;&#x7684;&#x989D;&#x5916;&#x4FE1;&#x606F;&#x5230;&#x91CC;&#x9762;&#x3002;&#x8981;&#x8BB0;&#x4F4F;&#x7684;&#x662F;&#x4FDD;&#x6301; JWT &#x7B80;&#x77ED;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x662F;&#x9700;&#x8981;&#x901A;&#x8FC7; HTTP header &#x4F20;&#x9012;&#x7684;&#x3002;  </p>
<p>JWT &#x5185;&#x6DB5;&#x7684;&#x4E30;&#x5BCC;&#x6027;&#x610F;&#x5473;&#x7740;&#x4F60;&#x53EF;&#x4EE5;&#x628A;&#x5B83;&#x5F53;&#x4F5C;&#x5B8C;&#x6574;&#x7684;&#x8BA4;&#x8BC1;&#x4FE1;&#x606F;&#x5728;&#x7CFB;&#x7EDF;&#x5185;&#x4F20;&#x9012;&#x3002;  </p>
<h3 id="&#x4F7F;&#x7528;&#x573A;&#x666F;">&#x4F7F;&#x7528;&#x573A;&#x666F;</h3>
<p>JWT &#x4EE4;&#x724C;&#x53EF;&#x4EE5;&#x88AB;&#x7528;&#x6765;&#x8BA4;&#x8BC1;&#x7CFB;&#x7EDF;&#x7684;&#x4EFB;&#x4F55;&#x4E00;&#x4E2A;&#x90E8;&#x5206;&#x3002;  </p>
<ul>
<li>&#x5355;&#x9875;&#x5E94;&#x7528;  </li>
<li>&#x63A7;&#x5236;&#x5668;&#xFF08;&#x901A;&#x8FC7;&#x6D4F;&#x89C8;&#x5668;&#x4F1A;&#x8BDD;&#xFF09;  </li>
<li>&#x63A7;&#x5236;&#x5668;&#xFF08;&#x901A;&#x8FC7; API &#x7684;&#x9A8C;&#x8BC1; headers&#xFF09;  </li>
<li>Phoenix Channels</li>
<li>&#x670D;&#x52A1;&#x4E4B;&#x95F4;&#x7684;&#x8BF7;&#x6C42;  </li>
<li>&#x8FDB;&#x7A0B;&#x95F4;&#x901A;&#x4FE1;  </li>
<li>&#x7B2C;&#x4E09;&#x65B9;&#x8BBF;&#x95EE;&#xFF08;OAuth&#xFF09;  </li>
<li>&#x201C;&#x8BB0;&#x4F4F;&#x6211;&#x201D;&#x529F;&#x80FD;  </li>
<li>&#x5176;&#x5B83;&#x63A5;&#x53E3; - &#x539F;&#x59CB;&#x7684; TCP&#xFF0C;UDP&#xFF0C;CLI &#x7B49;</li>
</ul>
<p>JWT &#x4EE4;&#x724C;&#x53EF;&#x4EE5;&#x7528;&#x5728;&#x4EFB;&#x4F55;&#x9700;&#x8981;&#x63D0;&#x4F9B;&#x53EF;&#x9A8C;&#x8BC1;&#x7684;&#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1;&#x4FE1;&#x606F;&#x4E4B;&#x5904;&#x3002;  </p>
<h3 id="&#x6211;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x6570;&#x636E;&#x5E93;&#x5417;&#xFF1F;">&#x6211;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x6570;&#x636E;&#x5E93;&#x5417;&#xFF1F;</h3>
<p>&#x4F60;&#x4E0D;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x6570;&#x636E;&#x5E93;&#x6765;&#x8DDF;&#x8E2A; JWT &#x7684;&#x4F7F;&#x7528;&#x60C5;&#x51B5;&#x3002;&#x7B80;&#x5355;&#x7684;&#x4F9D;&#x9760;&#x53D1;&#x653E;&#x65E5;&#x671F;&#x548C;&#x8FC7;&#x671F;&#x65F6;&#x95F4;&#x6765;&#x63A7;&#x5236;&#x8BBF;&#x95EE;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#x3002;&#x901A;&#x5E38;&#x53EF;&#x80FD;&#x4F60;&#x4F1A;&#x4F7F;&#x7528;&#x6570;&#x636E;&#x5E93;&#x6765;&#x67E5;&#x8BE2;&#x7528;&#x6237;&#x7684;&#x8D44;&#x6E90;&#xFF0C;&#x4F46;&#x662F; JWT &#x672C;&#x8EAB;&#x5E76;&#x4E0D;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x6570;&#x636E;&#x5E93;&#x3002;  </p>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x60F3;&#x4F7F;&#x7528; JWT &#x6765;&#x9A8C;&#x8BC1; UDP &#x901A;&#x4FE1;&#xFF0C;&#x4F60;&#x4E0D;&#x592A;&#x9700;&#x8981;&#x7528;&#x4E0A;&#x6570;&#x636E;&#x5E93;&#x3002;&#x5728;&#x53D1;&#x653E; JWT &#x7684;&#x65F6;&#x5019;&#xFF0C;&#x76F4;&#x63A5;&#x5305;&#x542B;&#x6240;&#x6709;&#x9700;&#x8981;&#x7684;&#x4FE1;&#x606F;&#x5230;&#x4EE4;&#x724C;&#x91CC;&#x9762;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#x3002;&#x53EA;&#x8981;&#x5B83;&#x662F;&#x53EF;&#x4FE1;&#x7684;&#xFF08;&#x68C0;&#x67E5;&#x5B83;&#x6709;&#x6B63;&#x786E;&#x7684;&#x7B7E;&#x540D;&#x4FE1;&#x606F;&#xFF09;&#xFF0C;&#x5C31;&#x6CA1;&#x95EE;&#x9898;&#x3002;  </p>
<p>&#x4F46;&#x662F;&#xFF0C;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x6570;&#x636E;&#x5E93;&#x6765;&#x8DDF;&#x8E2A; JWT&#x3002;&#x8FD9;&#x6837;&#x7684;&#x597D;&#x5904;&#x662F;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x9A8C;&#x8BC1;&#x4EE4;&#x724C;&#x662F;&#x5426;&#x8FD8;&#x662F;&#x6709;&#x6548;&#xFF0C;&#x6CA1;&#x6709;&#x88AB;&#x540A;&#x9500;&#x7684;&#x3002;&#x6216;&#x8005;&#x4F60;&#x8FD8;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x68C0;&#x67E5;&#x5B58;&#x50A8;&#x5728;&#x6570;&#x636E;&#x5E93;&#x7684;&#x8BB0;&#x5F55;&#xFF0C;&#x4E00;&#x6B21;&#x8FC7;&#x5F3A;&#x5236;&#x767B;&#x51FA;&#x6240;&#x6709;&#x7684;&#x7528;&#x6237;&#x3002;&#x4F7F;&#x7528; <a href="https://github.com/hassox/guardian_db" target="_blank">GuardianDb</a> &#x80FD;&#x975E;&#x5E38;&#x5BB9;&#x6613;&#x5730;&#x5B9E;&#x73B0;&#x4E0A;&#x9762;&#x7684;&#x529F;&#x80FD;&#x3002;GuardianDb &#x4F7F;&#x7528; Guardians &apos;Hooks&apos; &#x6765;&#x5B9E;&#x73B0;&#x9A8C;&#x8BC1;&#x68C0;&#x67E5;&#xFF0C;&#x6570;&#x636E;&#x5E93;&#x7684;&#x4FDD;&#x5B58;&#x548C;&#x5220;&#x9664;&#x64CD;&#x4F5C;&#x3002;&#x7A0D;&#x540E;&#x4F1A;&#x4ECB;&#x7ECD;&#x3002;  </p>
<h2 id="&#x914D;&#x7F6E;">&#x914D;&#x7F6E;</h2>
<p>&#x914D;&#x7F6E; Guardian &#x6709;&#x597D;&#x51E0;&#x79CD;&#x65B9;&#x5F0F;&#x3002;&#x6211;&#x4EEC;&#x90FD;&#x4F1A;&#x4E00;&#x4E00;&#x4ECB;&#x7ECD;&#xFF0C;&#x5148;&#x4ECE;&#x6700;&#x7B80;&#x5355;&#x7684;&#x5F00;&#x59CB;&#x3002;  </p>
<h3 id="&#x6700;&#x7B80;&#x914D;&#x7F6E;">&#x6700;&#x7B80;&#x914D;&#x7F6E;</h3>
<p>&#x4F60;&#x9700;&#x8981;&#x66F4;&#x6539;&#x597D;&#x51E0;&#x4E2A;&#x914D;&#x7F6E;&#x3002;  </p>
<h4 id="&#x6587;&#x4EF6;&#x914D;&#x7F6E;">&#x6587;&#x4EF6;&#x914D;&#x7F6E;</h4>
<p><code>mix.exs</code></p>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">application</span></span> <span class="hljs-keyword">do</span>
  [
    <span class="hljs-symbol">mod:</span> {MyApp, []},
    <span class="hljs-symbol">applications:</span> [<span class="hljs-symbol">:guardian</span>, ...]
  ]
<span class="hljs-keyword">end</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">deps</span></span> <span class="hljs-keyword">do</span>
  [
    {<span class="hljs-symbol">guardian:</span> <span class="hljs-string">&quot;~&gt; x.x&quot;</span>},
    ...
  ]
<span class="hljs-keyword">end</span>
</code></pre>
<p><code>config/config.ex</code></p>
<pre><code class="lang-elixir"><span class="hljs-comment"># &#x4FEE;&#x6539;&#x6240;&#x6709;&#x5916;&#x90E8;&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</span>
config <span class="hljs-symbol">:guardian</span>, Guardian,
  <span class="hljs-symbol">issuer:</span> <span class="hljs-string">&quot;MyAppId&quot;</span>,
  <span class="hljs-symbol">secret_key:</span> Mix.env(),
  <span class="hljs-symbol">serializer:</span> MyApp.GuardianSerializer
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5C31;&#x662F; Guardian &#x6240;&#x9700;&#x7684;&#x6700;&#x57FA;&#x672C;&#x7684;&#x4FE1;&#x606F;&#x3002;&#x628A;&#x5BC6;&#x94A5;&#xFF08;secret key&#xFF09;&#x76F4;&#x63A5;&#x5199;&#x5728;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x91CC;&#x9762;&#x5E76;&#x4E0D;&#x662F;&#x597D;&#x7684;&#x505A;&#x6CD5;&#xFF0C;&#x800C;&#x662F;&#x6BCF;&#x4E2A;&#x73AF;&#x5883;&#x5E94;&#x8BE5;&#x6709;&#x81EA;&#x5DF1;&#x7684;&#x5BC6;&#x94A5;&#x3002;&#x5728;&#x5F00;&#x53D1;&#x548C;&#x6D4B;&#x8BD5;&#x73AF;&#x5883;&#x4F7F;&#x7528; Mix &#x73AF;&#x5883;&#x53D8;&#x91CF;&#x662F;&#x6BD4;&#x8F83;&#x5E38;&#x89C1;&#x7684;&#x505A;&#x6CD5;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x5728;&#x9884;&#x53D1;&#x548C;&#x751F;&#x4EA7;&#x73AF;&#x5883;&#xFF0C;&#x5C31;&#x5FC5;&#x987B;&#x4F7F;&#x7528;&#x5F3A;&#x5BC6;&#x94A5;&#xFF08;&#x6BD4;&#x5982;&#xFF0C;&#x4F7F;&#x7528; <code>mix phoenix.gen.secret</code> &#x6765;&#x751F;&#x6210;&#xFF09;&#x3002;  </p>
<p><code>lib/my_app/guardian_serializer.ex</code></p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">MyApp</span></span>.GuardianSerializer <span class="hljs-keyword">do</span>
  <span class="hljs-variable">@behaviour</span> Guardian.Serializer

  <span class="hljs-keyword">alias</span> MyApp.Repo
  <span class="hljs-keyword">alias</span> MyApp.User

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">for_token</span></span>(user = %User{}), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:ok</span>, <span class="hljs-string">&quot;User:<span class="hljs-subst">#{user.id}</span>&quot;</span>}
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">for_token</span></span>(<span class="hljs-number">_</span>), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:error</span>, <span class="hljs-string">&quot;Unknown resource type&quot;</span>}

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">from_token</span></span>(<span class="hljs-string">&quot;User:&quot;</span> &lt;&gt; id), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:ok</span>, Repo.get(User, id)}
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">from_token</span></span>(<span class="hljs-number">_</span>), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:error</span>, <span class="hljs-string">&quot;Unknown resource type&quot;</span>}
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x4F60;&#x7684; seralizer &#x8D1F;&#x8D23;&#x6839;&#x636E; <code>sub</code>&#xFF08;&#x4E3B;&#x4F53;&#xFF09;&#x5B57;&#x6BB5;&#x627E;&#x5230;&#x76F8;&#x5E94;&#x7684;&#x8D44;&#x6E90;&#x3002;&#x5B83;&#x53EF;&#x4EE5;&#x662F;&#x4ECE;&#x6570;&#x636E;&#x5E93;&#x67E5;&#x627E;&#xFF0C;&#x8C03;&#x7528; API &#x83B7;&#x53D6;&#xFF0C;&#x6216;&#x8005;&#x53EA;&#x662F;&#x8FD4;&#x56DE;&#x7B80;&#x5355;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x3002;&#x5B83;&#x540C;&#x65F6;&#x4E5F;&#x8D1F;&#x8D23;&#x628A;&#x8D44;&#x6E90;&#x5E8F;&#x5217;&#x5316;&#x4E3A; <code>sub</code> &#x5B57;&#x6BB5;&#x503C;&#x3002;  </p>
<p>&#x8FD9;&#x5C31;&#x662F;&#x6700;&#x7B80;&#x914D;&#x7F6E;&#x4E86;&#x3002;&#x867D;&#x7136;&#x8FD8;&#x6709;&#x66F4;&#x591A;&#x5176;&#x5B83;&#x9009;&#x9879;&#xFF0C;&#x4F46;&#x662F;&#x8FD9;&#x6837;&#x8D77;&#x6B65;&#x5DF2;&#x7ECF;&#x8DB3;&#x591F;&#x4E86;&#x3002;  </p>
<h2 id="&#x5E94;&#x7528;&#x96C6;&#x6210;">&#x5E94;&#x7528;&#x96C6;&#x6210;</h2>
<p>&#x73B0;&#x5728; Guardian &#x7684;&#x914D;&#x7F6E;&#x5DF2;&#x7ECF;&#x51C6;&#x5907;&#x597D;&#x4E86;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x548C;&#x6211;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#x96C6;&#x6210;&#x8D77;&#x6765;&#x4E86;&#x3002;&#x6211;&#x4EEC;&#x5148;&#x770B;&#x770B;&#x5982;&#x4F55;&#x5728; HTTP &#x8BF7;&#x6C42;&#x4E2D;&#x4F7F;&#x7528;&#x3002;  </p>
<h3 id="http-&#x8BF7;&#x6C42;">HTTP &#x8BF7;&#x6C42;</h3>
<p>Guardian &#x63D0;&#x4F9B;&#x4E86;&#x597D;&#x4E00;&#x4E9B;&#x548C; HTTP &#x8BF7;&#x6C42;&#x96C6;&#x6210;&#x7684; Plugs&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x4ECE;<a href="../../specifics/plug">&#x53E6;&#x4E00;&#x7AE0;&#x8BFE;&#x7A0B;</a>&#x4E2D;&#x4E86;&#x89E3; Plug&#x3002;Guardian &#x4E0D;&#x662F;&#x5FC5;&#x987B;&#x548C; Phoenix &#x96C6;&#x6210;&#xFF0C;&#x4F46;&#x662F;&#x6700;&#x5BB9;&#x6613;&#x7684;&#x3002;  </p>
<p>&#x6700;&#x7B80;&#x5355;&#x7684;&#x548C; HTTP &#x96C6;&#x6210;&#x7684;&#x65B9;&#x5F0F;&#x662F;&#x901A;&#x8FC7;&#x8DEF;&#x7531;&#x5668;&#x3002;&#x56E0;&#x4E3A; Guardian &#x7684; HTTP &#x96C6;&#x6210;&#x662F;&#x57FA;&#x4E8E; plugs &#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x4EFB;&#x4F55;&#x7528; plug &#x7684;&#x5730;&#x65B9;&#x4F7F;&#x7528;&#x5B83;&#x3002;  </p>
<p>Guardian plug &#x4F7F;&#x7528;&#x7684;&#x4E00;&#x822C;&#x6D41;&#x7A0B;&#x662F;&#x8FD9;&#x6837;&#x7684;&#xFF1A;  </p>
<ol>
<li>&#x4ECE;&#x8BF7;&#x6C42;&#xFF08;&#x4EFB;&#x4F55;&#x5730;&#x65B9;&#xFF09;&#x4E2D;&#x627E;&#x5230;&#x4EE4;&#x724C;&#xFF0C;&#x5E76;&#x9A8C;&#x8BC1;&#xFF1A;<code>Verify*</code> plugs  </li>
<li>&#x53EF;&#x9009;&#x5730;&#x4ECE;&#x4EE4;&#x724C;&#x4E2D;&#x83B7;&#x53D6;&#x76F8;&#x5E94;&#x7684;&#x8D44;&#x6E90;&#xFF1A;<code>LoadResource</code> plug  </li>
<li>&#x786E;&#x4FDD;&#x8BF7;&#x6C42;&#x4E2D;&#x5305;&#x542B;&#x6709;&#x6548;&#x7684;&#x4EE4;&#x724C;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#xFF0C;&#x62D2;&#x7EDD;&#x8BBF;&#x95EE;&#xFF1A;<code>EnsureAuthenticated</code> plug</li>
</ol>
<p>&#x4E3A;&#x4E86;&#x6EE1;&#x8DB3;&#x5E94;&#x7528;&#x5F00;&#x53D1;&#x8005;&#x7684;&#x9700;&#x6C42;&#xFF0C;Guardian &#x5206;&#x5F00;&#x5B9E;&#x73B0;&#x4E86;&#x8FD9;&#x4E9B;&#x4E0D;&#x540C;&#x7684;&#x9636;&#x6BB5;&#x3002;&#x4F7F;&#x7528; <code>Verify*</code> plugs &#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x4EE4;&#x724C;&#x3002;  </p>
<p>&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E00;&#x4E9B; Phoenix &#x7BA1;&#x9053;&#xFF08;pipelines&#xFF09;&#x6765;&#x770B;&#x770B;&#x5982;&#x4F55;&#x64CD;&#x4F5C;&#x3002;  </p>
<pre><code class="lang-elixir">pipeline <span class="hljs-symbol">:maybe_browser_auth</span> <span class="hljs-keyword">do</span>
  plug(Guardian.Plug.VerifySession)
  plug(Guardian.Plug.VerifyHeader, <span class="hljs-symbol">realm:</span> <span class="hljs-string">&quot;Bearer&quot;</span>)
  plug(Guardian.Plug.LoadResource)
<span class="hljs-keyword">end</span>

pipeline <span class="hljs-symbol">:ensure_authed_access</span> <span class="hljs-keyword">do</span>
  plug(Guardian.Plug.EnsureAuthenticated, %{<span class="hljs-string">&quot;typ&quot;</span> =&gt; <span class="hljs-string">&quot;access&quot;</span>, <span class="hljs-symbol">handler:</span> MyApp.HttpErrorHandler})
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x8FD9;&#x4E9B;&#x7BA1;&#x9053;&#x53EF;&#x4EE5;&#x88AB;&#x5B9E;&#x73B0;&#x6210;&#x4E0D;&#x540C;&#x7684;&#x7EC4;&#x5408;&#x6765;&#x6EE1;&#x8DB3;&#x5404;&#x79CD;&#x9A8C;&#x8BC1;&#x7684;&#x4E1A;&#x52A1;&#x573A;&#x666F;&#x3002;&#x7B2C;&#x4E00;&#x4E2A;&#x9996;&#x5148;&#x5C1D;&#x8BD5;&#x4ECE;&#x4F1A;&#x8BDD;&#x4E2D;&#x83B7;&#x53D6;&#x4EE4;&#x724C;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x7684;&#x8BDD;&#x5C31;&#x4ECE; header &#x4ECE;&#x627E;&#x3002;&#x5982;&#x679C;&#x627E;&#x5230;&#x4E86;&#xFF0C;&#x5B83;&#x4F1A;&#x5E2E;&#x4F60;&#x5B9A;&#x4F4D;&#x5BF9;&#x5E94;&#x7684;&#x8D44;&#x6E90;&#x3002;  </p>
<p>&#x7B2C;&#x4E8C;&#x4E2A;&#x7BA1;&#x9053;&#x786E;&#x4FDD;&#x5FC5;&#x987B;&#x6709;&#x4E00;&#x4E2A;&#x6709;&#x6548;&#x7684;&#x4EE4;&#x724C;&#x5B58;&#x5728;&#xFF0C;&#x5E76;&#x4E14;&#x662F;&#x201C;access&#x201D;&#x7C7B;&#x578B;&#x3002;&#x628A;&#x8FD9;&#x4E9B; piplines &#x52A0;&#x5230; scope &#x91CC;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E86;&#x3002;  </p>
<pre><code class="lang-elixir">scope <span class="hljs-string">&quot;/&quot;</span>, MyApp <span class="hljs-keyword">do</span>
  pipe_through([<span class="hljs-symbol">:browser</span>, <span class="hljs-symbol">:maybe_browser_auth</span>])

  get(<span class="hljs-string">&quot;/login&quot;</span>, LoginController, <span class="hljs-symbol">:new</span>)
  post(<span class="hljs-string">&quot;/login&quot;</span>, LoginController, <span class="hljs-symbol">:create</span>)
  delete(<span class="hljs-string">&quot;/login&quot;</span>, LoginController, <span class="hljs-symbol">:delete</span>)
<span class="hljs-keyword">end</span>

scope <span class="hljs-string">&quot;/&quot;</span>, MyApp <span class="hljs-keyword">do</span>
  pipe_through([<span class="hljs-symbol">:browser</span>, <span class="hljs-symbol">:maybe_browser_auth</span>, <span class="hljs-symbol">:ensure_authed_access</span>])

  resource(<span class="hljs-string">&quot;/protected/things&quot;</span>, ProtectedController)
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684; login &#x8DEF;&#x7531;&#x5982;&#x679C;&#x53D1;&#x73B0;&#x8BF7;&#x6C42;&#x91CC;&#x5E26;&#x6709;&#x9A8C;&#x8BC1;&#x901A;&#x8FC7;&#x7684;&#x7528;&#x6237;&#x4FE1;&#x606F;&#x7684;&#x8BDD;&#xFF0C;&#x4F1A;&#x6CE8;&#x5165;&#x76F8;&#x5E94;&#x7684;&#x7528;&#x6237;&#x3002;&#x7B2C;&#x4E8C;&#x4E2A; scope &#x5219;&#x786E;&#x4FDD;&#x5FC5;&#x987B;&#x6709;&#x6709;&#x6548;&#x7684;&#x4EE4;&#x724C;&#x624D;&#x80FD;&#x901A;&#x8FC7;&#x540E;&#x7EED;&#x7684;&#x64CD;&#x4F5C;&#x3002;&#x4F60;&#x4E0D;&#x662F;<em>&#x5FC5;&#x987B;</em>&#x628A;&#x5B83;&#x4EEC;&#x914D;&#x7F6E;&#x5728;&#x7BA1;&#x9053;&#x4E2D;&#xFF0C;&#x4F60;&#x8FD8;&#x53EF;&#x4EE5;&#x5F88;&#x7075;&#x6D3B;&#x5730;&#x914D;&#x7F6E;&#x5230;&#x63A7;&#x5236;&#x5668;&#xFF08;controller&#xFF09;&#x91CC;&#x3002;&#x4E0D;&#x8FC7;&#x8FD9;&#x91CC;&#x662F;&#x6700;&#x7B80;&#x914D;&#x7F6E;&#xFF0C;&#x6240;&#x4EE5;&#x624D;&#x8FD9;&#x6837;&#x64CD;&#x4F5C;&#x3002;</p>
<p>&#x5F80;&#x4E8B;&#x5177;&#x5907;&#xFF0C;&#x53EA;&#x5DEE;&#x4E1C;&#x98CE;&#x4E86;&#x3002;&#x5C31;&#x662F;&#x6DFB;&#x52A0;&#x5230; <code>EnsureAuthenticated</code> plug &#x7684;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x3002;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x6A21;&#x5757;&#xFF1A;  </p>
<ul>
<li><code>unauthenticated/2</code></li>
<li><code>unauthorized/2</code></li>
</ul>
<p>&#x8FD9;&#x4E24;&#x4E2A;&#x51FD;&#x6570;&#x90FD;&#x540C;&#x6837;&#x63A5;&#x6536; Plug.Conn &#x8FD9;&#x4E2A;&#x7ED3;&#x6784;&#x4F53;&#xFF0C;&#x548C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x6620;&#x5C04;&#x8868;&#xFF0C;&#x5E76;&#x4E14;&#x5904;&#x7406;&#x76F8;&#x5E94;&#x7684;&#x9519;&#x8BEF;&#x3002;&#x4F60;&#x751A;&#x81F3;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; Phoenix &#x7684;&#x63A7;&#x5236;&#x5668;&#xFF01;  </p>
<h3 id="&#x548C;&#x63A7;&#x5236;&#x5668;&#x7ED3;&#x5408;">&#x548C;&#x63A7;&#x5236;&#x5668;&#x7ED3;&#x5408;</h3>
<p>&#x5728;&#x63A7;&#x5236;&#x5668;&#x91CC;&#xFF0C;&#x6709;&#x597D;&#x51E0;&#x79CD;&#x65B9;&#x5F0F;&#x4ECE;&#x4E2D;&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x767B;&#x5F55;&#x7684;&#x7528;&#x6237;&#x3002;&#x6211;&#x4EEC;&#x5148;&#x770B;&#x6700;&#x7B80;&#x5355;&#x7684;&#x65B9;&#x6CD5;&#x3002;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">MyApp</span></span>.MyController <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> MyApp.Web, <span class="hljs-symbol">:controller</span>
  <span class="hljs-keyword">use</span> Guardian.Phoenix.Controller

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">some_action</span></span>(conn, params, user, claims) <span class="hljs-keyword">do</span>
    <span class="hljs-comment"># do stuff</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x901A;&#x8FC7; <code>Guardian.Phoenix.Controller</code> &#x6A21;&#x5757;&#xFF0C;&#x4F60;&#x7684;&#x64CD;&#x4F5C;&#xFF08;actions&#xFF09;&#x53EF;&#x4EE5;&#x83B7;&#x5F97;&#x989D;&#x5916;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x3002;&#x8981;&#x8BB0;&#x4F4F;&#x7684;&#x662F;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x4F7F;&#x7528; <code>EnsureAuthenticated</code>&#xFF0C;&#x4F60;&#x83B7;&#x5F97;&#x7684;&#x53EF;&#x80FD;&#x662F;&#x7A7A;&#x7684; user &#x548C; claims &#x503C;&#x3002;  </p>
<p>&#x53E6;&#x4E00;&#x79CD;&#x66F4;&#x7075;&#x6D3B;&#xFF0C;&#x6216;&#x8005;&#x8BF4;&#x7E41;&#x7410;&#x7684;&#x65B9;&#x5F0F;&#x662F;&#xFF0C;&#x4F7F;&#x7528; plug &#x8F85;&#x52A9;&#x51FD;&#x6570;&#x3002;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">MyApp</span></span>.MyController <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> MyApp.Web, <span class="hljs-symbol">:controller</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">some_action</span></span>(conn, params) <span class="hljs-keyword">do</span>
    if Guardian.Plug.authenticated?(conn) <span class="hljs-keyword">do</span>
      user = Guardian.Plug.current_resource(conn)
    else
      <span class="hljs-comment"># No user</span>
    <span class="hljs-keyword">end</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<h3 id="&#x767B;&#x5165;&#x767B;&#x51FA;">&#x767B;&#x5165;/&#x767B;&#x51FA;</h3>
<p>&#x767B;&#x5165;&#xFF0C;&#x548C;&#x767B;&#x51FA;&#x6D4F;&#x89C8;&#x5668;&#x4F1A;&#x8BDD;&#x975E;&#x5E38;&#x5BB9;&#x6613;&#x5B9E;&#x73B0;&#x3002;&#x5728;&#x4F60;&#x7684; login &#x63A7;&#x5236;&#x5668;&#x91CC;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create</span></span>(conn, params) <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">case</span> find_the_user_and_verify_them_from_params(params) <span class="hljs-keyword">do</span>
    {<span class="hljs-symbol">:ok</span>, user} -&gt;
      <span class="hljs-comment"># Use access tokens.</span>
      <span class="hljs-comment"># Other tokens can be used, like :refresh etc</span>
      conn
      |&gt; Guardian.Plug.sign_in(user, <span class="hljs-symbol">:access</span>)
      |&gt; respond_somehow()

    {<span class="hljs-symbol">:error</span>, reason} -&gt;
      <span class="hljs-keyword">nil</span>
      <span class="hljs-comment"># handle not verifying the user&apos;s credentials</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete</span></span>(conn, params) <span class="hljs-keyword">do</span>
  conn
  |&gt; Guardian.Plug.sign_out()
  |&gt; respond_somehow()
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x5F53;&#x901A;&#x8FC7; API &#x767B;&#x5F55;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5904;&#x7406;&#x7684;&#x65B9;&#x5F0F;&#x4F1A;&#x6709;&#x70B9;&#x4E0D;&#x540C;&#x3002;&#x56E0;&#x4E3A;&#x5F53;&#x524D;&#x5E76;&#x6CA1;&#x6709;&#x4F1A;&#x8BDD;&#xFF0C;&#x4F60;&#x8FD8;&#x5FC5;&#x987B;&#x628A;&#x539F;&#x59CB;&#x7684;&#x4EE4;&#x724C;&#x8FD4;&#x56DE;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x3002;&#x8981;&#x5904;&#x7406; API &#x767B;&#x5F55;&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x4ECE; <code>Authorization</code> header &#x4E2D;&#x83B7;&#x53D6;&#x4EE4;&#x724C;&#x3002;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x5BF9;&#x4E0D;&#x9700;&#x8981;&#x4FDD;&#x6301;&#x548C;&#x4F7F;&#x7528;&#x4F1A;&#x8BDD;&#x662F;&#x53EF;&#x884C;&#x7684;&#x3002;  </p>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create</span></span>(conn, params) <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">case</span> find_the_user_and_verify_them_from_params(params) <span class="hljs-keyword">do</span>
    {<span class="hljs-symbol">:ok</span>, user} -&gt;
      {<span class="hljs-symbol">:ok</span>, jwt, _claims} = Guardian.encode_and_sign(user, <span class="hljs-symbol">:access</span>)
      conn |&gt; respond_somehow(%{<span class="hljs-symbol">token:</span> jwt})

    {<span class="hljs-symbol">:error</span>, reason} -&gt;
      <span class="hljs-comment"># handle not verifying the user&apos;s credentials</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete</span></span>(conn, params) <span class="hljs-keyword">do</span>
  jwt = Guardian.Plug.current_token(conn)
  Guardian.revoke!(jwt)
  respond_somehow(conn)
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x6D4F;&#x89C8;&#x5668;&#x4F1A;&#x8BDD;&#x767B;&#x5F55;&#x5B9E;&#x9645;&#x4E0A;&#x4F1A;&#x8C03;&#x7528; <code>encode_and_sign</code> &#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x6240;&#x4EE5;&#x4F60;&#x8FD9;&#x6837;&#x8C03;&#x7528;&#x4E5F;&#x662F;&#x53EF;&#x4EE5;&#x7684;&#x3002;  </p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../specifics/debugging.html" class="navigation navigation-prev " aria-label="Previous page: 调试">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="poolboy.html" class="navigation navigation-next " aria-label="Next page: Poolboy">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.0.3","title":"Guardian(基础)","level":"5.1","depth":1,"next":{"title":"Poolboy","level":"5.2","depth":1,"path":"lessons/libraries/poolboy.md","ref":"lessons/libraries/poolboy.md","articles":[]},"previous":{"title":"调试","level":"4.5","depth":1,"path":"lessons/specifics/debugging.md","ref":"lessons/specifics/debugging.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/libraries/guardian.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

